//
//  XNTravelViewController.m
//  XINNingOA
//
//  Created by samuel on 2021/7/14.
//

#import "XNTravelViewController.h"
#import "XNTravelBillInfoModel.h"
#import "XNTravelBillInfoRequest.h"

@interface XNTravelViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) XNTravelBillInfoModel *infoModel;

@property (nonatomic, copy) NSArray *approveArrayC;


@end

@implementation XNTravelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [SVProgressHUD show];
    __weak typeof(self)weakSelf = self;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_group_t group = dispatch_group_create();
    dispatch_group_enter(group);
    XNTravelBillInfoRequest *infoRequest = [[XNTravelBillInfoRequest alloc] initWithOrderId:self.orderId];
    [infoRequest startWithCompletionBlockWithSuccess:^(__kindof XNTravelBillInfoRequest * _Nonnull request) {
        weakSelf.infoModel = request.infoModel;
        __block NSMutableArray *arrayA = [NSMutableArray array];
        __block NSMutableArray *arrayC = [NSMutableArray array];
        [weakSelf.infoModel.historyList enumerateObjectsUsingBlock:^(XNBillApproveModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            obj.checkAccountUserName = obj.checkAccountName;
            if ([obj.checkType isEqualToString:@"A"]) {
                obj.seq = arrayA.count + 1;
                [arrayA addObject:obj];
            }else{
                obj.seq = arrayC.count + 1;
                [arrayC addObject:obj];
            }
        }];
        weakSelf.approveArray = [NSArray arrayWithArray:arrayA];
        weakSelf.approveArrayC = [NSArray arrayWithArray:arrayC];
        dispatch_group_leave(group);
    } failure:^(__kindof XNTravelBillInfoRequest * _Nonnull request) {
        dispatch_group_leave(group);
    }];
    
//    dispatch_group_enter(group);
//    XNAdministrationAffExpApproveRequest *approveRequest = [[XNAdministrationAffExpApproveRequest alloc] initWithOrderId:self.orderId];
//    [approveRequest startWithCompletionBlockWithSuccess:^(__kindof XNAdministrationAffExpApproveRequest * _Nonnull request) {
//        weakSelf.approveArray = request.approveFlowArray;
//        dispatch_group_leave(group);
//    } failure:^(__kindof XNAdministrationAffExpApproveRequest * _Nonnull request) {
//        dispatch_group_leave(group);
//    }];
//    
    dispatch_group_notify(group, queue, ^{
        [weakSelf creatTableData];

        dispatch_async(dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
            [weakSelf.tableView reloadData];
        });

    });
    
}

- (void)creatTableData
{
    XNTableShowDataModel *infoModel = [[XNTableShowDataModel alloc] init];
    infoModel.showType = XNShowType_Normal;
    infoModel.title = @"单据信息";
    XNShowItemModel *itemModel = [[XNShowItemModel alloc] init];
    itemModel.showOverImage = self.infoModel.budgetItemObject.remainApplyMoney < 0;
    NSArray *showItemArray = [NSArray arrayWithObjects:
                              [NSString stringWithFormat:@"单据编号： %@",self.infoModel.ID],
                              [NSString stringWithFormat:@"制单日期： %@",self.infoModel.createTimeStr],
                              [NSString stringWithFormat:@"制单人： %@",self.infoModel.createUserObject.userName],
                              [NSString stringWithFormat:@"制单部门： %@",self.infoModel.createUserObject.departmentName],
                              [NSString stringWithFormat:@"职务： %@",self.infoModel.createUserObject.positionName],
                              nil];
    itemModel.showItemArray = showItemArray;
    [infoModel.showDataArray addObject:itemModel];
    [self.showArray addObject:infoModel];
    
    XNTableShowDataModel *submitApplyModel = [[XNTableShowDataModel alloc] init];
    submitApplyModel.showType = XNShowType_Normal;
    submitApplyModel.title = @"个人信息";
    XNShowItemModel *itemModel2 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray2 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"申请人: %@",self.infoModel.applicantObject.userName],
                               [NSString stringWithFormat:@"申请部门： %@",self.infoModel.applicantObject.departmentName],
                               [NSString stringWithFormat:@"职位： %@",self.infoModel.applicantObject.positionName],
                               [NSString stringWithFormat:@"性别： %@",self.infoModel.applicantObject.genderStr],
                               [NSString stringWithFormat:@"联系电话： %@",self.infoModel.applicantObject.telephone],
                               @"附件信息:",
                               nil];
    itemModel2.showItemArray = showItemArray2;
    itemModel2.fileArray = self.infoModel.fileList;
    itemModel2.fileIsImage = NO;
    [submitApplyModel.showDataArray addObject:itemModel2];
    [self.showArray addObject:submitApplyModel];
    
    XNTableShowDataModel *showModel3 = [[XNTableShowDataModel alloc] init];
    showModel3.showType = XNShowType_Normal;
    showModel3.title = @"行程计划";
    XNShowItemModel *itemModel3 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray3;
    if (!self.infoModel.isStay) {
        showItemArray3 = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"出差日期: %@",self.infoModel.travelRange],
                                   [NSString stringWithFormat:@"费用承担部门： %@",self.infoModel.departmentCostName],
                                   [NSString stringWithFormat:@"是否住宿： %@",self.infoModel.isStay? @"是":@"否"],
                                   [NSString stringWithFormat:@"出发地： %@",self.infoModel.startAddress],
                                   [NSString stringWithFormat:@"目的地： %@",self.infoModel.endAddress],
                                   [NSString stringWithFormat:@"出差补助标准： %@",self.infoModel.travelTypeName.length > 0 ? self.infoModel.travelTypeName : @""],
                                   nil];
    }else{
        showItemArray3 = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"出差日期: %@",self.infoModel.travelRange],
                                   [NSString stringWithFormat:@"费用承担部门： %@",self.infoModel.departmentCostName],
                                   [NSString stringWithFormat:@"是否住宿： %@",self.infoModel.isStay? @"是":@"否"],
                                   [NSString stringWithFormat:@"出发地： %@",self.infoModel.startAddress],
                                   [NSString stringWithFormat:@"目的地： %@",self.infoModel.endAddress],
                          [NSString stringWithFormat:@"出差补助标准： %@",self.infoModel.travelTypeName.length > 0 ? self.infoModel.travelTypeName : @""],
                          [NSString stringWithFormat:@"住宿方式： %@",self.infoModel.stayTypeName.length > 0 ? self.infoModel.stayTypeName : @""],
                                      [NSString stringWithFormat:@"住宿日期： %@",self.infoModel.stayRangeStr],
                                      [NSString stringWithFormat:@"住宿天数： %@",self.infoModel.stayDays],
                                   nil];
    }

    itemModel3.showItemArray = showItemArray3;
    [showModel3.showDataArray addObject:itemModel3];
    [self.showArray addObject:showModel3];
    
    XNTableShowDataModel *showModel4 = [[XNTableShowDataModel alloc] init];
    showModel4.showType = XNShowType_Normal;
    showModel4.title = @"工作计划";
    [self.infoModel.workPlanList enumerateObjectsUsingBlock:^(XNWorkPlanModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *itemModel4 = [[XNShowItemModel alloc] init];
        NSArray *showItemArray4 = [NSArray arrayWithObjects:
                                       [NSString stringWithFormat:@"日期起迄: %@",obj.workPlanDateStr],
                                       [NSString stringWithFormat:@"涉及部门： %@",obj.relatedDepartment],
                                       [NSString stringWithFormat:@"相关人员： %@",obj.relatedPeople],
                                       [NSString stringWithFormat:@"具体工作项目或目标： %@",obj.relatedContent],
                                       nil];
        itemModel4.showItemArray = showItemArray4;
        [showModel4.showDataArray addObject:itemModel4];
    }];
    [self.showArray addObject:showModel4];
    
    
    XNTableShowDataModel *reimbursementModel = [[XNTableShowDataModel alloc] init];
    reimbursementModel.showType = XNShowType_MoreData;
    reimbursementModel.title = @"报销明细";
    reimbursementModel.showDataArray = [self creatSubTableData];

    [self.showArray addObject:reimbursementModel];
    
    
    XNTableShowDataModel *showModel5 = [[XNTableShowDataModel alloc] init];
    showModel5.showType = XNShowType_Normal;
    showModel5.title = @"差旅费用总计";
    XNShowItemModel *itemModel5 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray5;
    if (![self.infoModel.status isEqualToString:@"BS090"]) {
        showItemArray5 = [NSArray arrayWithObjects:
                          [NSString stringWithFormat:@"本次差旅费用总计： %.3f",self.infoModel.travelExpenseCost],
                                       nil];
    }else{
        showItemArray5 = [NSArray arrayWithObjects:
                                       [NSString stringWithFormat:@"本次差旅费用总计： %.3f",self.infoModel.travelExpenseCost],
                                       [NSString stringWithFormat:@"实报差旅费用总计： %.3f",self.infoModel.travelExpensePayCost],
                                       nil];
    }
    itemModel5.showItemArray = showItemArray5;
    [showModel5.showDataArray addObject:itemModel5];
    [self.showArray addObject:showModel5];
    
    
//    XNTableShowDataModel *showModel6 = [[XNTableShowDataModel alloc] init];
//    showModel6.showType = XNShowType_Normal;
//    showModel6.title = @"交际费用总计";
//    XNShowItemModel *itemModel6 = [[XNShowItemModel alloc] init];
//    NSArray *showItemArray6;
//    if (![self.infoModel.status isEqualToString:@"BS090"]) {
//        showItemArray6 = [NSArray arrayWithObjects:
//                          [NSString stringWithFormat:@"本次交际费用总计： %.3f",self.infoModel.comTotalCost],
//                                       nil];
//    }else{
//        showItemArray6 = [NSArray arrayWithObjects:
//                                       [NSString stringWithFormat:@"本次差旅费用总计： %.3f",self.infoModel.comTotalCost],
//                                       [NSString stringWithFormat:@"实报交际费用总计：%.3f",self.infoModel.comTotalPayCost],
//                                       nil];
//    }
//    itemModel6.showItemArray = showItemArray6;
//    [showModel6.showDataArray addObject:itemModel6];
//    [self.showArray addObject:showModel6];
    
    
    if (self.infoModel.isTemp) {
        XNTableShowDataModel *reimbursementModel = [[XNTableShowDataModel alloc] init];
        reimbursementModel.showType = XNShowType_Normal;
        reimbursementModel.title = @"暂支单";
        XNShowItemModel *itemModel3 = [[XNShowItemModel alloc] init];
        NSArray *showItemArray3 = [NSArray arrayWithObjects:
                                   [NSString stringWithFormat:@"暂支事由： %@",self.infoModel.advanceDesc],
                                   [NSString stringWithFormat:@"暂支金额： %.3f",self.infoModel.advance],
                                   [NSString stringWithFormat:@"预计归还日期： %@",self.infoModel.advanceDateStr],
                                   nil];
        itemModel3.showItemArray = showItemArray3;
        [reimbursementModel.showDataArray addObject:itemModel3];
        [self.showArray addObject:reimbursementModel];
    }
    
    XNTableShowDataModel *showModel7 = [[XNTableShowDataModel alloc] init];
    showModel7.showType = XNShowType_Normal;
    showModel7.title = @"预算信息";
    XNShowItemModel *itemModel7 = [[XNShowItemModel alloc] init];
    NSArray *showItemArray7 = [NSArray arrayWithObjects:
                               [NSString stringWithFormat:@"【合并核扣】差旅费（%@）预算总金额:%.3f",self.infoModel.budgetItemObject.budgetInfo,self.infoModel.budgetItemObject.allMoney],
                               [NSString stringWithFormat:@"已报销： %.3f",self.infoModel.budgetItemObject.reimbursedMoney],
                               [NSString stringWithFormat:@"报销中： %.3f",self.infoModel.budgetItemObject.reimbursingMoney],
                               [NSString stringWithFormat:@"剩余可报销： %.3f",self.infoModel.budgetItemObject.remainReimburseMoney],
                               [NSString stringWithFormat:@"已申请未报销： %.3f",self.infoModel.budgetItemObject.applyedMoney],
                               [NSString stringWithFormat:@"在申请未报销： %.3f",self.infoModel.budgetItemObject.applyingMoney],
                               [NSString stringWithFormat:@"剩余可申请： %.3f",self.infoModel.budgetItemObject.remainApplyMoney],
                               nil];
    itemModel7.showItemArray = showItemArray7;
    [showModel7.showDataArray addObject:itemModel7];
    [self.showArray addObject:showModel7];
    
    
    XNTableShowDataModel *approveModelA = [[XNTableShowDataModel alloc] init];
    approveModelA.showType = XNShowType_Approve;
    approveModelA.title = @"审批流程";
    [self.approveArray enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *temp = [[XNShowItemModel alloc] init];
        temp.showItemArray = [NSArray arrayWithObject:obj];
        [approveModelA.showDataArray addObject:temp];
    }];
    [self.showArray addObject:approveModelA];
    
    if (self.approveArrayC.count > 0) {
        XNTableShowDataModel *approveModelC = [[XNTableShowDataModel alloc] init];
        approveModelC.showType = XNShowType_Approve;
        approveModelC.title = @"报销审核流程";
        [self.approveArrayC enumerateObjectsUsingBlock:^(XNBillApproveModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
            XNShowItemModel *temp = [[XNShowItemModel alloc] init];
            temp.showItemArray = [NSArray arrayWithObject:obj];
            [approveModelC.showDataArray addObject:temp];
        }];
        [self.showArray addObject:approveModelC];
    }
}

- (NSMutableArray *)creatSubTableData
{
    NSMutableArray *array = [NSMutableArray array];
    
    XNTableShowDataModel *showData1 = [[XNTableShowDataModel alloc] init];
//    __block CGFloat total1 = 0;
    showData1.title = @"出差费";
    [self.infoModel.expenseList enumerateObjectsUsingBlock:^(XNExpenseModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        XNShowItemModel *item = [[XNShowItemModel alloc] init];
        NSString *tempStr = @"";
        if (obj.seq == 5) {
            tempStr = [NSString stringWithFormat:@"%@补助",self.infoModel.subsidy ? @"无":@"有"];
        }
        if([self.infoModel.status isEqualToString:@"BS090"]){
            item.showItemArray = [NSArray arrayWithObjects:
                                  [NSString stringWithFormat:@"费用项目：%@%@", obj.name,tempStr],
                                  [NSString stringWithFormat:@"费用说明：%@",obj.costContent],
                                  [NSString stringWithFormat:@"日期起迄：%@",obj.expenseDateRange],
                                  [NSString stringWithFormat:@"申请旅费：%.3f",obj.cost],
                                  [NSString stringWithFormat:@"实报旅费：%.3f",obj.payCost],
                                  [NSString stringWithFormat:@"附件张数：%@",obj.fileCount],
                                  [NSString stringWithFormat:@"摘要：%@",obj.remark],
                                  nil];

        }else{
            item.showItemArray = [NSArray arrayWithObjects:
                                  [NSString stringWithFormat:@"费用项目：%@%@", obj.name,tempStr],
                                  [NSString stringWithFormat:@"费用说明：%@",obj.costContent],
                                  [NSString stringWithFormat:@"日期起迄：%@",obj.expenseDateRange],
                                  [NSString stringWithFormat:@"申请旅费：%.3f",obj.cost],
                                  nil];
        }
        [showData1.showDataArray addObject:item];

    }];
    [array addObject:showData1];
    
    
//    XNTableShowDataModel *showData2 = [[XNTableShowDataModel alloc] init];
//    __block CGFloat total2 = 0;
//    showData2.title = @"交际费";
//    [self.infoModel.comList enumerateObjectsUsingBlock:^(XNComModel * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//        XNShowItemModel *item = [[XNShowItemModel alloc] init];
//        NSArray *temArray;
//        if([self.infoModel.status isEqualToString:@"BS090"]){
//            temArray = [NSArray arrayWithObjects:
//                                  [NSString stringWithFormat:@"交际日期：%@",obj.communicationDateStr],
//                                  [NSString stringWithFormat:@"交际类型：%@",obj.typeName],
//                                  [NSString stringWithFormat:@"交际形式：%@",obj.wayName],
//                                  [NSString stringWithFormat:@"交际说明：%@",obj.communicationContent],
//                                  [NSString stringWithFormat:@"地点：%@",obj.address],
//                                  [NSString stringWithFormat:@"客户姓名：%@",obj.name],
//                                  [NSString stringWithFormat:@"客户职务：%@",obj.duty],
//                                  [NSString stringWithFormat:@"客户等级：%@",obj.customerLevelStr],
//                                  [NSString stringWithFormat:@"与公司来往职责范围：%@",obj.dutyRange],
//                                  [NSString stringWithFormat:@"公司参加人员：%@",obj.attendPeople],
//                                  [NSString stringWithFormat:@"申请金额：%.3f",obj.cost],
//                                  [NSString stringWithFormat:@"实报金额：%.3f",obj.payCost],
//                                  [NSString stringWithFormat:@"附件张数：%ld",obj.fileCount],
//                                  [NSString stringWithFormat:@"摘要：%@",obj.remark],
//                                  nil];
//        }else{
//            temArray = [NSArray arrayWithObjects:
//                                  [NSString stringWithFormat:@"交际日期：%@",obj.communicationDateStr],
//                                  [NSString stringWithFormat:@"交际类型：%@",obj.typeName],
//                                  [NSString stringWithFormat:@"交际形式：%@",obj.wayName],
//                                  [NSString stringWithFormat:@"交际说明：%@",obj.communicationContent],
//                                  [NSString stringWithFormat:@"地点：%@",obj.address],
//                                  [NSString stringWithFormat:@"客户姓名：%@",obj.name],
//                                  [NSString stringWithFormat:@"客户职务：%@",obj.duty],
//                                  [NSString stringWithFormat:@"客户等级：%@",obj.customerLevelStr],
//                                  [NSString stringWithFormat:@"与公司来往职责范围：%@",obj.dutyRange],
//                                  [NSString stringWithFormat:@"公司参加人员：%@",obj.attendPeople],
//                                  [NSString stringWithFormat:@"申请金额：%.3f",obj.cost],
//                                  nil];
//        }
//        item.showItemArray = temArray;
////        total2 = total2 + obj.cost;
//        [showData2.showDataArray addObject:item];
//    }];
////    showData2.headTitleArray = [NSMutableArray arrayWithObjects:
////                      [NSString stringWithFormat:@"合计金额：%.3f",total2],
////                      nil];
//    [array addObject:showData2];
    

    
    return array;
}


@end
